<template>
  <div class="vue-box view-9812321">
    <div class="c-panel login">
      <!-- 参数栏 -->
      <div class="c-title" style="color: #0466c5;text-align: center"><h1>修改密码</h1></div>
      <el-form size="mini">
        <el-form-item label="请输入旧密码：">
          <el-input type="password" v-model="pwd" clearable></el-input>
        </el-form-item>
          <el-form-item label="请输入新密码：">
            <el-input type="password" v-model="newpwd" clearable></el-input>
          </el-form-item>
            <el-form-item label="请在输入一次新密码：">
              <el-input type="password" v-model="newpwd2" clearable></el-input>
            </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="ok()">提交</el-button>
          <el-button type="danger" @click="pwd = newpwd = newpwd2 = null">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {encrypt} from '@/static/aes'
export default {
  name: 'password',
  data () {
    return {
      pwd: null,
      newpwd: null,
      newpwd2: null,
    }
  },
  methods : {
    submit_check: function() {
      if(this.sa.isNull(this.pwd)) {
        return this.sa.error('请输入旧密码!');
      }
      if(this.sa.isNull(this.newpwd)) {
        return this.sa.error('请输入新密码!');
      }
      if(this.sa.isNull(this.newpwd2)) {
        return this.sa.error('请在输入一次新密码!');
      }
      var reg = /^(?![\\d]+$)(?![a-zA-Z]+$)(?![^\\da-zA-Z]+$)([^\u4e00-\u9fa5\\s]){6,20}$/
      if(!reg.test(this.newpwd)) {
        return this.sa.error('新密码要求6-20位英文字母、数字或者符号（除空格），且字母、数字和标点符号至少包含两种!');
      }
      if(this.newpwd != this.newpwd2) {
        return this.sa.error('新密码两次输入不一致');
      }
      return 'ok';
    },
    // 提交
    ok: function() {
      if(this.submit_check() != 'ok') {
        return
      } else {
        //将密码进行加密
        let p = encrypt(this.pwd);
        this.sa.ajax('/login/updatePwd',
            {'pwd' : p, 'newPwd' : this.newpwd},
            function () {
              this.sa.ok('修改密码成功,请使用新密码重新登录!')
              this.sa.clearAuth();
              localStorage.removeItem('tokenName');
              localStorage.removeItem('tokenValue');
              localStorage.setItem('name', "未登录");
              this.sa_admin.$nextTick()
              this.sa_admin.openLogin();
            }.bind(this)
        )
      }
    },
  }
}
</script>

<style scoped>
.login{
  width: 400px;
  height: 300px;
  /* 把元素变成定位元素 */
  position: absolute;
  /* 元素距离上，左都为50% */
  left: 50%;
  top: 30%;
  /* 让元素的左外边距，上外边距为元素宽高的1/2 注意margin是负距*/
  margin-top: -150px;
  margin-left: -200px;
  text-align: center
}
</style>
